home *** CD-ROM | disk | FTP | other *** search
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // FontMaxWidth.c
- // Written by Donald Olson
- // Copyright ®1993 Apple Computer Inc.
- // All rights reserved.
- //
- // This is a simple Scripting Addition that returns the font max width of the
- // specified font and size. If the size parameter is not sp[ecified, default
- // size of 12 is used. Written for a WWDC presentation by Donald Olson
- // and Donn Denman.
- //
- // Syntax: font max width <font name [size <short>]
- // Returns: short
- //
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-
- // Our includes
- #include <string.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Errors.h>
- #include <AppleEvents.h>
-
- // Our header
- Boolean GetFontNumber( Str255 fontName, short *fontNum );
-
- /*
- This is the structure returned from FontInfo.
- struct FontInfo {
- short ascent;
- short descent;
- short widMax;
- short leading;
- };
- */
- */
- //AEVTFINFmxwd
- #define keyFONTSIZE 'FSIZ'
-
- pascal OSErr main(AppleEvent *theEvent,
- AppleEvent *theReply,
- long theRefCon)
- {
- OSErr theErr = noErr;
- GrafPort fontPort;
- GrafPtr savedPort, fontPortPtr = &fontPort;
- Str255 fontName;
- long actualSize;
- short fontSize, fontNum;
- DescType typeCode;
- FontInfo fInfo;
-
- theErr = AEGetParamPtr(theEvent, keyDirectObject,
- typeChar, &typeCode, (Ptr)&fontName,
- sizeof(fontName), &actualSize);
-
- if(theErr != noErr)
- return theErr;
-
- fontName[actualSize] = '\0'; // c string please
- c2pstr((char*)fontName); // and now pascal
-
- theErr = AEGetParamPtr(theEvent, keyFONTSIZE,
- typeShortInteger, &typeCode, (Ptr)&fontSize,
- sizeof(fontSize), &actualSize);
-
- if(theErr != noErr)
- fontSize = 12;
-
- if(!GetFontNumber(fontName, &fontNum))
- return fontDecError;
-
- GetPort(&savedPort);
- OpenPort(&fontPortPtr);
- SetPort(&fontPortPtr);
-
- TextFont(fontNum);
- TextSize(fontSize);
-
- GetFontInfo(&fInfo);
-
- SetPort(&savedPort);
-
- theErr = AEPutParamPtr( theReply, keyDirectObject, typeShortInteger,
- (Ptr)&fInfo.widMax, sizeof(fInfo.widMax));
-
- return theErr;
- }
-
- Boolean GetFontNumber( Str255 fontName, short *fontNum )
- {
- Str255 systemFontName;
-
- GetFNum( fontName, fontNum );
- if ( *fontNum == 0) {
- /* Either we didn't find the font, or we were looking for the system
- * font. */
- GetFontName( 0, systemFontName );
- return EqualString( fontName, systemFontName, FALSE, FALSE );
- }
- else
- return TRUE;
- }
-